package com.neurotech.pcc.other;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by shenhua on 2019/2/18.
 *
 * @author shenhua
 * Email shenhuanet@126.com
 */
public class ThreadPool {

    private ThreadPoolExecutor threadPoolExecutor;
    private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

    private static ThreadPool sInstance = null;

    public static ThreadPool get() {
        if (sInstance == null) {
            synchronized (ThreadPool.class) {
                if (sInstance == null) {
                    sInstance = new ThreadPool();
                }
            }
        }
        return sInstance;
    }

    private ThreadPool() {
        if (threadPoolExecutor == null) {
            threadPoolExecutor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
        }
    }

    public ThreadPoolExecutor thread() {
        return threadPoolExecutor;
    }

    public void resetScheduled() {
        stopScheduled();
        scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(100);
    }

    public ScheduledThreadPoolExecutor scheduled() {
        return scheduledThreadPoolExecutor;
    }

    public void stopScheduled() {
        if (scheduledThreadPoolExecutor != null) {
            scheduledThreadPoolExecutor.shutdown();
            scheduledThreadPoolExecutor = null;
        }
    }
}
